/**
 * Independent time operation tool to facilitate subsequent switch to dayjs
 */

export function formatDate  (value,args) {
  var dt = new Date(value);
  if(args == 'yyyy-M-d') {// yyyy-M-d
    let year = dt.getFullYear();
    let month = dt.getMonth() + 1;
    let date = dt.getDate();
    return `${year}-${month}-${date}`;
  } else if(args == 'yyyy-M-d H:m:s'){// yyyy-M-d H:m:s
    let year = dt.getFullYear();
    let month = dt.getMonth() + 1;
    let date = dt.getDate();
    let hour = dt.getHours();
    let minute = dt.getMinutes();
    let second = dt.getSeconds();
    return `${year}-${month}-${date} ${hour}:${minute}:${second}`;
  } else if(args == 'yyyy-MM-dd') {// yyyy-MM-dd
    let year = dt.getFullYear();
    let month = (dt.getMonth() + 1).toString().padStart(2,'0');
    let date = dt.getDate().toString().padStart(2,'0');
    return `${year}-${month}-${date}`;
  } else {// yyyy-MM-dd HH:mm:ss
    let year = dt.getFullYear();
    let month = (dt.getMonth() + 1).toString().padStart(2,'0');
    let date = dt.getDate().toString().padStart(2,'0');
    let hour = dt.getHours().toString().padStart(2,'0');
    let minute = dt.getMinutes().toString().padStart(2,'0');
    let second = dt.getSeconds().toString().padStart(2,'0');
    return `${year}-${month}-${date} ${hour}:${minute}:${second}`;
  }
}



export function dateRange (t, t1) {
  console.log("1212");
  let range = ''
  let sj = new Date(t)
  let sj1 = new Date(t1)
  let y = sj.getFullYear()
  let m = sj.getMonth() + 1
  let d = sj.getDate()
  let y1 = sj1.getFullYear()
  let m1 = sj1.getMonth() + 1
  let d1 = sj1.getDate()
  let month = Math.abs((y1 - y) * 12 + (m1 - m))
  // 扩展,计算两个日期相差多少年多少月多少天
  let y2 = Math.floor(month / 12)
  // 天数:如果d1<d,d1=30+d1,然后mont-1.如果d1>d,就直接d1-d
  let mont = month % 12
  let day2 = Math.abs(d1 - d)
  if (d1 < d) {
    d1 = d1 + 30
    day2 = Math.abs(d1 - d)
    mont = mont - 1
  }
  if (y2 > 0) range += y2 + '年'
  if (mont > 0) range += mont*30
  if (day2 > 0) range += day2 + ''
  return range
}

